package com.micro.controller;

import com.micro.entity.HotelOrder;
import com.micro.service.HotelOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/api/hotel-orders")
public class HotelOrderController {

    @Autowired
    private HotelOrderService hotelOrderService;

    // 创建酒店订单
    @PostMapping
    public ResponseEntity<HotelOrder> createOrder(@RequestBody HotelOrder order) {
        try {
            HotelOrder createdOrder = hotelOrderService.createOrder(order);
            return ResponseEntity.ok(createdOrder);
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }

    // 根据用户ID获取订单列表
    @GetMapping("/user/{userId}")
    public ResponseEntity<List<HotelOrder>> getOrdersByUserId(@PathVariable Long userId) {
        try {
            List<HotelOrder> orders = hotelOrderService.getOrdersByUserId(userId);
            return ResponseEntity.ok(orders);
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }

    // 根据酒店ID获取订单列表
    @GetMapping("/hotel/{hotelId}")
    public ResponseEntity<List<HotelOrder>> getOrdersByHotelId(@PathVariable Long hotelId) {
        try {
            List<HotelOrder> orders = hotelOrderService.getOrdersByHotelId(hotelId);
            return ResponseEntity.ok(orders);
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }

    // 根据订单状态获取订单列表
    @GetMapping("/status/{orderStatus}")
    public ResponseEntity<List<HotelOrder>> getOrdersByOrderStatus(@PathVariable HotelOrder.OrderStatus orderStatus) {
        try {
            List<HotelOrder> orders = hotelOrderService.getOrdersByOrderStatus(orderStatus);
            return ResponseEntity.ok(orders);
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }

    // 根据用户ID和订单状态获取订单列表
    @GetMapping("/user/{userId}/status/{orderStatus}")
    public ResponseEntity<List<HotelOrder>> getOrdersByUserIdAndOrderStatus(
            @PathVariable Long userId,
            @PathVariable HotelOrder.OrderStatus orderStatus) {
        try {
            List<HotelOrder> orders = hotelOrderService.getOrdersByUserIdAndOrderStatus(userId, orderStatus);
            return ResponseEntity.ok(orders);
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }

    // 根据入住日期获取订单列表
    @GetMapping("/checkin/{checkInDate}")
    public ResponseEntity<List<HotelOrder>> getOrdersByCheckInDate(
            @PathVariable @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate checkInDate) {
        try {
            List<HotelOrder> orders = hotelOrderService.getOrdersByCheckInDate(checkInDate);
            return ResponseEntity.ok(orders);
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }

    // 根据退房日期获取订单列表
    @GetMapping("/checkout/{checkOutDate}")
    public ResponseEntity<List<HotelOrder>> getOrdersByCheckOutDate(
            @PathVariable @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate checkOutDate) {
        try {
            List<HotelOrder> orders = hotelOrderService.getOrdersByCheckOutDate(checkOutDate);
            return ResponseEntity.ok(orders);
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }

    // 根据酒店ID和入住日期范围获取订单列表
    @GetMapping("/hotel/{hotelId}/date-range")
    public ResponseEntity<List<HotelOrder>> getOrdersByHotelIdAndCheckInDateBetween(
            @PathVariable Long hotelId,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        try {
            List<HotelOrder> orders = hotelOrderService.getOrdersByHotelIdAndCheckInDateBetween(hotelId, startDate, endDate);
            return ResponseEntity.ok(orders);
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }

    // 根据订单ID获取订单详情
    @GetMapping("/{orderId}")
    public ResponseEntity<HotelOrder> getOrderById(@PathVariable Long orderId) {
        try {
            HotelOrder order = hotelOrderService.getOrderById(orderId);
            if (order != null) {
                return ResponseEntity.ok(order);
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }

    // 更新订单状态
    @PutMapping("/{orderId}/status")
    public ResponseEntity<HotelOrder> updateOrderStatus(
            @PathVariable Long orderId,
            @RequestParam HotelOrder.OrderStatus orderStatus) {
        try {
            HotelOrder updatedOrder = hotelOrderService.updateOrderStatus(orderId, orderStatus);
            if (updatedOrder != null) {
                return ResponseEntity.ok(updatedOrder);
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }

    // 退款
    @PostMapping("/{orderId}/refund")
    public ResponseEntity<HotelOrder> refundOrder(
            @PathVariable Long orderId,
            @RequestParam BigDecimal refundAmount) {
        try {
            HotelOrder refundedOrder = hotelOrderService.refundOrder(orderId, refundAmount);
            if (refundedOrder != null) {
                return ResponseEntity.ok(refundedOrder);
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }
}
